PHP substr interception string gargle problem solution [utf8 and gb2312]

  • 2020-05-10 17:53:12
  • OfStack

substr -- gets part of the string
Grammar: string substr (string string, int start [, int length])
Description:
substr() returns a part 1 of string string, specified by the parameters start and length.
If start is positive, the string returned will start with the start character of string.
Example :
 
<?php 
$rest = substr ("abcdef", 1); // returns "bcdef" 
$rest = substr ("abcdef", 1, 3); // returns "bcd" 
?> 

If start is negative, the returned string will start at start at the end of string.
Example :
 
<?php 
$rest = substr ("abcdef", -1); // returns "f" 
$rest = substr ("abcdef", -2); // returns "ef" 
$rest = substr ("abcdef", -3, 1); // returns "d" 
?> 

If the parameter length is given and is positive, the returned string will return length characters from start.
If the parameter length is given and is negative, the returned string ends at the length character at the end of string.
Example :
 
<?php 
$rest = substr ("abcdef", 1, -1); // returns "bcde" 
?> 

dongyue,2005-01-07 11:10:41
substr -- gets part of the string
Grammar: string substr (string string, int start [, int length])
Description:
substr() returns a partial string of string, specified by the parameters start and length.
If start is positive, the string returned will start at the start character of string.
Example :
 
<?php 
$rest = substr ("abcdef", 1); // returns "bcdef" 
$rest = substr ("abcdef", 1, 3); // returns "bcd" 
?> 

If start is negative, the returned string will start with the start word at the end of string.
Example :
 
<?php 
$rest = substr ("abcdef", -1); // returns "f" 
$rest = substr ("abcdef", -2); // returns "ef" 
$rest = substr ("abcdef", -3, 1); // returns "d" 
?> 

If the parameter length is given and is positive, the returned string will return length characters from start.
If the parameter length is given and is negative, the returned string ends at the length character at the end of string.
Example :
 
<?php 
$rest = substr ("abcdef", 1, -1); // returns "bcde" 
?> 

Utf-8, gb2312 support the interception function of Chinese characters
 
// Intercept Chinese string  
/* 
Utf-8 , gb2312 Support Chinese character interception function  
cut_str( string ,  Intercept length ,  Start the length ,  coding ); 
 The default encoding is  utf-8 
 The starting length defaults to  0 
*/function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') 
{ 
if($code == 'UTF-8') 
{ 
$pa ="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; 
preg_match_all($pa, $string, $t_string); if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen)); 
return join('', array_slice($t_string[0], $start, $sublen)); 
} 
else 
{ 
$start = $start*2; 
$sublen = $sublen*2; 
$strlen = strlen($string); 
$tmpstr = ''; for($i=0; $i<$strlen; $i++) 
{ 
if($i>=$start && $i<($start+$sublen)) 
{ 
if(ord(substr($string, $i, 1))>129) 
{ 
$tmpstr.= substr($string, $i, 2); 
} 
else 
{ 
$tmpstr.= substr($string, $i, 1); 
} 
} 
if(ord(substr($string, $i, 1))>129) $i++; 
} 
if(strlen($tmpstr)<$strlen ) $tmpstr.= ""; 
return $tmpstr; 
} 
} 
$str=" The home of the script 1 Nice website "; 
echo cut_str($str, 8, 5, 'gb2312'); 

Related articles: